package dao.impl;

import com.mysql.cj.util.StringUtils;
import dao.BaseDao;
import dao.Check_OutDao;
import entity.Check;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Check_OutDaoImpl extends BaseDao implements Check_OutDao {

    @Override
    public List<Check> show(String student_number, int page, int pageSize) throws SQLException, ClassNotFoundException {
        getConnection();
        PreparedStatement statement = null;
        ResultSet set = null;
        List<Check> list = new ArrayList<>();

        try {


            // 确保分页参数有效
            if (page < 1) page = 1;
            if (pageSize < 1) pageSize = 10;
            int start = (page - 1) * pageSize;

            String sql = "SELECT s.student_number, a.checkout_time, a.operator, a.checkout_type, a.detail " +
                    "FROM checkout_records a, students s WHERE a.student_id = s.student_id ";
            if (!StringUtils.isNullOrEmpty(student_number)) {
                sql += "AND s.student_number = ? ";
            }
            sql += "LIMIT ?, ?";

            statement = conn.prepareStatement(sql);
            int paramIndex = 1;
            if (!StringUtils.isNullOrEmpty(student_number)) {
                statement.setString(paramIndex++, student_number);
            }
            statement.setInt(paramIndex++, start);
            statement.setInt(paramIndex, pageSize);

            set = statement.executeQuery();
            while (set.next()) {
                Check check = new Check();
                check.setStudent_number(set.getString("student_number"));
                check.setCheckout_time(set.getString("checkout_time"));
                check.setOperator(set.getString("operator"));
                check.setCheckout_type(set.getString("checkout_type"));
                check.setDetail(set.getString("detail"));
                list.add(check);
            }
            return list;
        } finally {
            // 关闭资源（ResultSet → PreparedStatement → Connection）
            if (set != null) {
                try {
                    set.close();
                } catch (SQLException e) {
                    e.printStackTrace(); // 实际项目中建议使用日志框架
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public int getTotalCount(String student_number) throws SQLException, ClassNotFoundException {
        getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;

        try {

            String sql = "SELECT COUNT(*) FROM checkout_records a, students s WHERE a.student_id = s.student_id ";
            if (!StringUtils.isNullOrEmpty(student_number)) {
                sql += "AND s.student_number = ?";
            }

            statement = conn.prepareStatement(sql);
            if (!StringUtils.isNullOrEmpty(student_number)) {
                statement.setString(1, student_number);
            }

            rs = statement.executeQuery();
            return rs.next() ? rs.getInt(1) : 0;
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}